//
//  SCCodeLoginController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/5/29.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCCodeLoginController.h"

@interface SCCodeLoginController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UIButton    *codeBtn;

@property (assign, nonatomic) NSInteger timeCount;
@property (strong, nonatomic) NSTimer   *timer;
@property (strong, nonatomic) NSString  *smsno;

@end

@implementation SCCodeLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"手机验证码登录";
}
//获取验证码
- (IBAction)clickCodeBtn:(UIButton *)sender {
    if (self.phoneTF.text.length == 0) {
        [SVProgressHUD showImage:nil status:@"请输入您的手机号码"];
        return;
    }
    [self.view endEditing:YES];
    
    NSDictionary *dict = @{@"mobile":self.phoneTF.text};
    NSString *url = [MyTool splicingWithUrl:@"login/app/index.php?i=2&c=entry&m=jack_sys&do=auth&op=sendauthsm"];
    [SVProgressHUD showWithStatus:@"加载中..."];
    
    [[NetworkRequestTool sharedNetworkTool] GET:url parameters:dict finished:^(id result, NSString *error) {
        //NSLog(@"----%@",result);
        [SVProgressHUD showImage:nil status:@"验证码已发送"];
        
        self.smsno = result[@"data"][@"smsno"];
        //self.codeTF.text = result[@"data"][@"psw"];
        
        sender.enabled = NO;
        [self.codeBtn setBackgroundColor:RGB(200, 200, 200)];
        self.timeCount = 60;
        NSString *str = [NSString stringWithFormat:@"(%lus)重新获取", (long)self.timeCount];
        [self.codeBtn setTitle:str forState:0];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reduceTime:) userInfo:nil repeats:YES];
    }];
}
- (void)reduceTime:(NSTimer *)codeTimer {
    if (self.timeCount == 0) {
        [self.codeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.codeBtn setBackgroundColor:RGB(255, 152, 1)];
        self.codeBtn.enabled = YES;
        [self.timer invalidate];
        self.timer = nil;
    } else {
        self.timeCount--;
        NSString *str = [NSString stringWithFormat:@"(%lus)重新获取", (long)self.timeCount];
        [self.codeBtn setTitle:str forState:UIControlStateNormal];
        self.codeBtn.enabled = NO;
    }
}
//登录
- (IBAction)clickLoginBtn:(UIButton *)sender {
    [self.view endEditing:YES];
    if (self.phoneTF.text.length != 11) {
        [SVProgressHUD showImage:nil status:@"请输入您的手机号码"];
        return;
    }
    if (self.codeTF.text.length == 0) {
        [SVProgressHUD showImage:nil status:@"请输入收到的验证码"];
        return;
    }
    NSDictionary *dict = @{@"phone":self.phoneTF.text,
                           @"smsno":self.smsno,
                           @"dypsw":self.codeTF.text};
    NSString *url = [MyTool splicingWithUrl:@"login/app/index.php?i=2&c=entry&m=jack_sys&do=auth&op=autho"];
    [SVProgressHUD showWithStatus:@"登录中..."];
    
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        //NSLog(@"登录成功返回的数据%@",result);
        [SVProgressHUD showSuccessWithStatus:@"登录成功"];
        NSUserDefaults *uf = [NSUserDefaults standardUserDefaults];
        [uf setObject:result[@"data"][@"newtoken"] forKey:@"token"];
        [uf synchronize];
        [self.navigationController popViewControllerAnimated:NO];
        if (self.loginSuccess) {
            self.loginSuccess();
        }
    }];
}

@end
